//
//  JCWRiskAssessmentResultView.swift
//  HengYouCai
//
//  Created by wll on 2017/12/7.
//  Copyright © 2017年 lwj. All rights reserved.
//  风险评测视图--代码

import UIKit

class JCWRiskAssessmentResultView: UIView {
    
    deinit {
        debugPrint("JCWRiskAssessmentResultView 释放了")
    }

    ///视图样式
    private var type: JCWRiskAssessmentResultViewType = .big
    ///风险投资测评结果
    private var status: JCWRiskAssessmentResultType = .jqx
    ///回调函数
    var callBack: ((_ index: Int, _ btnTitle: String)->Void)?
    
    ///更新内容 str htmlStr代码 用tv展示
    func updateContentTV(_ str: String?, _ status: JCWRiskAssessmentResultType?) {
        //处理图片
        self.status = status ?? .jqx
        self.tyImV.image = UIImage(named: self.status.rawImageNamed)
        
        //处理描述
        guard let tempStr = str else {return}
        if let data = tempStr.data(using: String.Encoding.unicode) {
            do {
                let text = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSForegroundColorAttributeName: Normal_666Color], documentAttributes: nil)
                self.contentTV.attributedText = text
                
                self.setNeedsLayout()
                self.layoutIfNeeded()
            } catch  {
                debugPrint("html2attribute fail")
            }
        }
    }
    
    //MARK:外部函数 更新描述
    
    ///更新内容(两段文字) 传入str1 String, str2htmlStr代码 用tv展示
    func updateContentTV(_ str1: String?, _ str2: String?, _ status: JCWRiskAssessmentResultType?) {
        //处理图片
        self.status = status ?? .jqx
        self.tyImV.image = UIImage(named: self.status.rawImageNamed)
        
        
        //处理描述
        guard let tempStr1 = str1, let tempStr2 = str2 else {return}
        if let data = tempStr2.data(using: String.Encoding.unicode) {
            do {
                let text = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSForegroundColorAttributeName: Normal_666Color], documentAttributes: nil)
                let totalStr: NSMutableAttributedString = NSMutableAttributedString(string: tempStr1+"\n\n", attributes: [NSForegroundColorAttributeName: Normal_666Color])
                totalStr.append(text)
                self.contentTV.attributedText = totalStr
                
                self.setNeedsLayout()
                self.layoutIfNeeded()
            } catch  {
                debugPrint("html2attribute fail")
            }
        }
    }
    
    
    ///MARK:构造函数
    
    ///默认进取型 big模式
    @discardableResult init(_ status: JCWRiskAssessmentResultType?, _ type: JCWRiskAssessmentResultViewType?, _ callBack: ((_ index: Int, _ btnTitle: String)-> Void)?) {
        super.init(frame: CGRect.zero)
        self.type = type ?? .big
        self.callBack = callBack
        self.status = status ?? .jqx
        self.createUI()
    }
    ///默认进取型 big模式
    @discardableResult init(_ frame: CGRect, _ status: JCWRiskAssessmentResultType?, _ type: JCWRiskAssessmentResultViewType?, _ callBack: ((_ index: Int, _ btnTitle: String)-> Void)?) {
        super.init(frame: frame)
        self.type = type ?? .big
        self.callBack = callBack
        self.status = status ?? .jqx
        self.createUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    ///添加UI
    private func createUI() {
        self.backgroundColor = UIColor.white
        self.layer.cornerRadius = 10
        if self.type != JCWRiskAssessmentResultViewType.smallNoImage {//无图无标题时不加载
            self.addSubview(self.titleLab)
            
            self.addSubview(self.leftArrowImV)
            
            self.addSubview(self.rightArrowImV)
            
            self.addSubview(self.tyImV)
        }
        
        self.addSubview(self.contentTV)
        
        self.addSubview(self.bottomTopBtn)

        self.addSubview(self.bottomBottomBtn)
        self.bottomBottomBtn.setTitle(self.type != .big ? "一键默认": "重新测评", for: .normal)
        
        if self.type == JCWRiskAssessmentResultViewType.smallNoImage {
            self.bottomTopBtn.backgroundColor = UIColor.white
            self.bottomTopBtn.setTitleColor(Normal_333Color, for: .normal)
            self.bottomTopBtn.setTitle("立即测评", for: .normal)
            
            self.bottomBottomBtn.setTitleColor(UIColor.white, for: .normal)
            self.bottomBottomBtn.backgroundColor = Normal_RedColor
            self.bottomBottomBtn.layer.cornerRadius = 20
        }
        
        //进取型隐藏
        self.bottomBottomBtn.isHidden = self.status == .jqx
    }
    
    //MARK:布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if self.type != JCWRiskAssessmentResultViewType.smallNoImage {
            self.titleLab.top_sd = self.type != .big ? 30 : 41
            self.titleLab.centerX_sd = self.width_sd/2.0
            
            self.leftArrowImV.centerY = self.titleLab.centerY
            self.leftArrowImV.right_sd = self.titleLab.left_sd - 8
            
            self.rightArrowImV.centerY = self.titleLab.centerY
            self.rightArrowImV.left_sd = self.titleLab.right_sd + 8
            
            self.tyImV.bounds = CGRect(x: 0, y: 0, width: 163, height: 163)
            self.tyImV.centerX_sd = self.width_sd/2.0
            self.tyImV.top_sd = self.titleLab.bottom_sd + 30
            
            self.contentTV.frame = CGRect(x: 10, y: self.tyImV.bottom_sd + 23, width: self.width_sd - 20, height: 1)
            self.contentTV.sizeToFit()
            
            self.bottomTopBtn.bounds = CGRect(x: 0, y: 0, width: self.width_sd - 50, height: 40)
            self.bottomTopBtn.centerX_sd = self.width_sd/2.0
            self.bottomTopBtn.top_sd = self.contentTV.bottom_sd + (self.type != .big ? 43 : 35)
            
        }else {
            self.contentTV.frame = CGRect(x: 10, y: 30, width: self.width_sd - 20, height: 1)
            self.contentTV.sizeToFit()
            
            self.bottomTopBtn.bounds = CGRect(x: 0, y: 0, width: self.width_sd - 50, height: 40)
            self.bottomTopBtn.centerX_sd = self.width_sd/2.0
            self.bottomTopBtn.top_sd = self.contentTV.bottom_sd + 59
        }
        
        self.bottomBottomBtn.bounds = CGRect(x: 0, y: 0, width:  self.width_sd - 50, height: 40)
        self.bottomBottomBtn.centerX_sd = self.width_sd/2.0
        self.bottomBottomBtn.top_sd = self.bottomTopBtn.bottom_sd + 5
        
    }
    
    
    ///MARK:懒加载
    
    ///标题左箭头
    lazy var leftArrowImV: UIImageView = {
        let temp = UIImageView(image: UIImage(named: "risk_left"))
        return temp
    }()
    
    ///标题右箭头
    lazy var rightArrowImV: UIImageView = {
        let temp = UIImageView(image: UIImage(named: "risk_right"))
        return temp
    }()
    
    ///标题
    lazy var titleLab: UILabel = {
        let temp = UILabel(frame: CGRect.zero)
        temp.textColor = Normal_333Color
        temp.font = Font_WL(15)
        temp.text = "您的风险测评结果为"
        temp.sizeToFit()
        return temp
    }()
    
    ///类型图片
    lazy var tyImV: UIImageView = {
        let temp = UIImageView()
        temp.image = UIImage(named: "risk_aggressive")
        return temp
    }()
    
    ///内容
    lazy var contentTV: UITextView = {
        let temp = UITextView(frame: CGRect.zero)
        temp.allowsEditingTextAttributes = false
        temp.isEditable = false
        temp.isScrollEnabled = false
        temp.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0.1, right: 0)
        return temp
    }()
    
    ///底部上按钮
    lazy var bottomTopBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.titleLabel?.font = Font_WL(15)
        temp.setTitleColor(UIColor.white, for: .normal)
        temp.setTitle("完成", for: .normal)
        temp.backgroundColor = Normal_RedColor
        temp.layer.cornerRadius = 20
        temp.tag = -1
        temp.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        return temp
    }()
    
    ///底部下按钮
    lazy var bottomBottomBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.titleLabel?.font = Font_WL(15)
        temp.setTitleColor(Normal_666Color, for: .normal)
        temp.setTitle("重新检测", for: .normal)
        temp.tag = -2
        temp.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        return temp
    }()
    
    ///MARK:按钮点击事件
    func btnClick(sender: UIButton) {
        self.callBack?(sender.tag, sender.currentTitle ?? "")
    }
    
}
